﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxGridView;
using DevExpress.Web.ASPxClasses;

public partial class DxExpress_AspxGridView : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Session["cgvCustomerList"] = null;//请空缓存
            Session["cgvQueryString"] = null;
            RequestSearch();
        }
        else
        {
            if (Session["cgvCustomerList"] != null && this.UserList.IsCallback)
            {
                RequestSearch();
            }
        }
    }

    /// <summary>
    /// 客户查询
    /// </summary>
    private void RequestSearch()
    {
        List<WFT_Batch_CustomerClassification> list;
        if (Session["cgvCustomerList"] == null)
        {//如果缓存为空 重新查询数据库
            list = GetCustomerList();
            Session["cgvCustomerList"] = list;
        }
        else
        {//读取缓存数据 以免在没有点击查询的情况下分页、排序会使用新的搜索条件
            list = (List<WFT_Batch_CustomerClassification>)Session["cgvCustomerList"];
        }

        BindCustomerList(list);
    }

    /// <summary>
    /// 绑定客户列表
    /// </summary>
    /// <param name="list">客户列表</param>
    private void BindCustomerList(List<WFT_Batch_CustomerClassification> list)
    {
        if (null == list)
            list = new List<WFT_Batch_CustomerClassification>();
        this.UserList.DataSource = list;
        this.UserList.DataBind();
        this.UserList.Columns[0].FixedStyle = GridViewColumnFixedStyle.Left;
        this.UserList.Columns[1].FixedStyle = GridViewColumnFixedStyle.Left;
        this.UserList.Columns[2].FixedStyle = GridViewColumnFixedStyle.Left;
        //统计信息
        //SetSummaryInfo(list);
    }

    private List<WFT_Batch_CustomerClassification> GetCustomerList()
    {
        List<WFT_Batch_CustomerClassification> list = new List<WFT_Batch_CustomerClassification>(100);
        for (int i = 0; i < 100; i++)
        {
            WFT_Batch_CustomerClassification c = new WFT_Batch_CustomerClassification();
            c.GUID = Guid.NewGuid().ToString();
            c.客户姓名 = i+"姓名";
            c.客户类别 = new Random().Next(1) == 0 ? "添加" : "导入";
            c.FundAccount = Guid.NewGuid().ToString();
            list.Add(c);
        }
        return list;
    }


    protected void UserList_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
    {
        if (!string.IsNullOrEmpty(e.Parameters))
        {
            string[] paras = e.Parameters.Split('|');
            if (paras.Length > 1)//排序，用以修改为第一次点击时实现desc排序
            {
                string sortColumn = paras[1];
                GridViewDataColumn col = (sender as ASPxGridView).Columns[sortColumn] as GridViewDataColumn;
                DevExpress.Data.ColumnSortOrder sortOrder = DevExpress.Data.ColumnSortOrder.Descending;
                if (col.SortOrder == DevExpress.Data.ColumnSortOrder.Descending)
                    sortOrder = DevExpress.Data.ColumnSortOrder.Ascending;
                (sender as ASPxGridView).ClearSort();
                col.SortOrder = sortOrder;
            }
        }
    }

    protected void UserList_CustomColumnDisplayText(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewColumnDisplayTextEventArgs e)
    {//显示格式化
        string source = e.GetFieldValue("客户类别").ToString();
        string fa = e.GetFieldValue("FundAccount").ToString();
        string nonesale = (e.GetFieldValue("是否不动户") == null) ? "" : e.GetFieldValue("是否不动户").ToString();
        switch (e.Column.Name)
        {
            case "2"://证件号码
                if (string.Compare(source, "手动添加") == 0)
                {
                    e.DisplayText = "";
                }
                break;
            case "4"://账户分析
                if (string.IsNullOrEmpty(fa)
                    || string.Compare(nonesale, "Y") == 0)
                {
                    e.DisplayText = "";
                }
                else
                {
                    string nav = "/FinancialManager/Account/HoldFundAnalyze.aspx?fa={0}";
                    e.DisplayText = "<a href='" + string.Format(nav, fa) + "'>查看</a>";
                }
                break;
            case "11"://年龄
                if (e.Value.ToString() == "-1")
                {
                    e.DisplayText = "--";
                }
                break;
            case "17"://是否基金账户
                if (e.Value != null && e.Value.ToString() == "Y")
                {
                    e.DisplayText = "基金帐户";
                }
                else
                {
                    e.DisplayText = "非基金账户";
                }
                break;
            case "5"://现有盈亏金额
            case "6"://现有收益率
            case "18"://现有盈亏金额
            case "19"://现有收益率
            case "20"://现有一次性投入收益
            case "21"://现有一次性投入收益率
            case "22"://现有定投收益
            case "23"://现有定投收益率
                Double nowLot;
                Double.TryParse(e.GetFieldValue("持有份额").ToString(), out nowLot);
                if (nowLot < 0.00001)
                {
                    e.DisplayText = "--";
                }
                break;
            case "10"://基金占比
            case "32"://金融资产
            case "33"://基金占比
            case "34"://人民币存款
            case "35"://人民币存款占比
            case "36"://第三方存管
            case "37"://第三方存管占比
            case "38"://理财产品
            case "39"://理财产品占比
            case "40"://纸黄金
            case "41"://纸黄金占比
            case "42"://外币存款
            case "43"://外币存款占比
            case "44"://保险
            case "45"://保险占比
            case "46"://国债
            case "47"://国债占比
                if (string.Compare(source, "手动添加") == 0)
                {
                    e.DisplayText = "--";
                }
                break;
        }
    }
}
